<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<!--    <el-form-item label="预约编码" prop="subCode">-->
<!--      <el-input v-model="dataForm.subCode" placeholder="预约编码"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="菜名" prop="dishName">
      <el-input v-model="dataForm.dishName" placeholder="菜名"></el-input>
    </el-form-item>
<!--    <el-form-item label="下单时间" prop="orderTime">-->
<!--      <el-input v-model="dataForm.orderTime" placeholder="下单时间"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="金额" prop="orderMoney">
      <el-input v-model="dataForm.orderMoney" placeholder="金额"></el-input>
    </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input v-model="dataForm.idCard" placeholder="身份证"></el-input>
      </el-form-item>
    <el-form-item label="备注" prop="memo">
      <el-input v-model="dataForm.memo" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          subCode: '',
          dishName: '',
          orderTime: '',
          orderMoney: '',
          idCard: '',
          memo: ''
        },
        dataRule: {
          // subCode: [
          //   { required: true, message: '预约编码不能为空', trigger: 'blur' }
          // ],
          dishName: [
            { required: true, message: '菜名不能为空', trigger: 'blur' }
          ],
          // orderTime: [
          //   { required: true, message: '下单时间不能为空', trigger: 'blur' }
          // ],
          orderMoney: [
            { required: true, message: '金额不能为空', trigger: 'blur' }
          ],
          memo: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              // url: this.$http.adornUrl(`/menu/subscribe/info/${this.dataForm.id}`),
              url: `http://localhost:8081/menu/subscribe/info/${this.dataForm.id}`,
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.subCode = data.subscribe.subCode
                this.dataForm.dishName = data.subscribe.dishName
                this.dataForm.orderTime = data.subscribe.orderTime
                this.dataForm.orderMoney = data.subscribe.orderMoney
                this.dataForm.memo = data.subscribe.memo
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              // url: this.$http.adornUrl(`/menu/subscribe/${!this.dataForm.id ? 'save' : 'update'}`),
              url: `http://localhost:8081/menu/subscribe/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'subCode': this.dataForm.subCode,
                'dishName': this.dataForm.dishName,
                'orderTime': this.dataForm.orderTime,
                'orderMoney': this.dataForm.orderMoney,
                'idCard': this.dataForm.idCard,
                'memo': this.dataForm.memo
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
